<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author lzw
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleImport()" v-auth="'operating:createLawsuitFile:edit'">
          <Icon icon="i-ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
        <a-button type="primary" @click="handleForm({})" v-auth="'operating:createLawsuitFile:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.dataSource }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsOperatingCreateLawsuitFileList">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { createLawsuitFileDelete, createLawsuitFileListData } from '/@/api/operating/createLawsuitFile';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { useModal } from '/@/components/Modal';
  import FormImport from './formImport.vue';

  const { t } = useI18n('operating.createLawsuitFile');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('诉讼材料生成管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('数据来源'),
        field: 'dataSource',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('数据来源'),
      dataIndex: 'dataSource',
      key: 'a.data_source',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('生成日期'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('生成数量'),
      dataIndex: 'createNumber',
      key: 'a.create_number',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('缺少材料项'),
      dataIndex: 'deficiency',
      key: 'a.deficiency',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('需检查项'),
      dataIndex: 'isCheckNumber',
      key: 'a.is_check_number',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('生成地址'),
      dataIndex: 'filePath',
      key: 'a.file_path',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑诉讼材料生成'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'operating:createLawsuitFile:edit',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除诉讼材料生成'),
        popConfirm: {
          title: t('是否确认删除诉讼材料生成'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'operating:createLawsuitFile:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload }] = useTable({
    api: createLawsuitFileListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { id: record.id };
    const res = await createLawsuitFileDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }
</script>
